﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace FasterWPF
{
    public class HorizontalPanelComposite : IComposite
    {
        public bool IsGrid()
        {
            return false;
        }

        public void RemoveFromChain(Border bdr) //unhook all the children so they may be re-added later, if desired
        {
            GetContainer<StackPanel>(bdr).Children.Clear();
        }

        public C GetContainer<C>(Border bdr)
    where C : FrameworkElement
        {
            return ((C)(((ContentControl)(bdr.Child)).Content));
        }

        public C GetContainerFromChildContentControl<C>(ContentControl chain) where C : FrameworkElement
        {
            return ((C)(chain.Content));
        }

        public T GetParent<T>(ContentControl sender)
      where T : FrameworkElement
        {
            return (T)((ContentControl)sender).GetParentFromContentControlOfComposite<T>();
        }

        public Panel AddChild<T>(T source, System.Windows.Controls.ContentControl chain, int row, int col, System.Windows.FrameworkElement child)
               where T : FrameworkElement
        {
            StackPanel g = ((StackPanel)(chain.Content));
            g.Children.Add(child);

            return (Panel)g;
        }

        private static StackPanel applyHorizontalPanelSettings(HorizontalPanelSettings wSet, StackPanel w)
        {
            if (wSet.Height != 0.0D)
            {
                w.Height = wSet.Height;
            }
            return w;
        }

        public void AddCompositeToContentControl<T>(T chain, ContentControl contentControl, object settings)
             where T : FrameworkElement
        {
            StackPanel childPanel = new StackPanel();
            childPanel.Orientation = Orientation.Horizontal;

            HorizontalPanelSettings gs = null;

            if (settings != null)
            {
                gs = (settings as HorizontalPanelSettings);
            }
            else
            {
                if (SettingsManager.ContainsSetting<T>(chain, "HorizontalPanelSettings"))
                {
                    gs = SettingsManager.GetSetting<T, HorizontalPanelSettings>(chain, "HorizontalPanelSettings");
                }
                else
                {
                    //default settings
                    gs = new HorizontalPanelSettings() { Height = 0.0D };
                }
            }
            applyHorizontalPanelSettings(gs, childPanel);
            
            contentControl.Content = childPanel;
        }

        public T GetParentFromChild<T>(FrameworkElement childElement) where T : FrameworkElement
        {
            return (T)((ContentControl)((StackPanel)((FrameworkElement)childElement.Parent)).Parent).GetParentFromContentControlOfComposite<T>();
        }

        public System.Windows.Controls.Border GetBorder<T>(System.Windows.FrameworkElement childElement)
        {
            Border tempResults = null;
            if (((FrameworkElement)childElement).Parent is ToolBarTray)
            {
                Dictionary<string, object> composites = ((ToolBarTray)((FrameworkElement)childElement).Parent).GetCompositeLookupDictionary<ToolBarTray>();

                foreach (KeyValuePair<string, object> obj in composites)
                {
                    Border br = ((Border)obj.Value);
                    UIElementCollection uiec = ((StackPanel)((ContentControl)br.Child).Content).Children;
                    foreach (UIElement uie in uiec)
                    {
                        if (uie is FrameworkElement)
                        {
                            if (childElement == ((FrameworkElement)uie).Tag)
                            {
                                tempResults = br;
                            }
                        }
                    }
                }
            }
            else
            { 
                tempResults = (Border)((StackPanel)((FrameworkElement)childElement).Parent).Parent.Cast<ContentControl>().Parent;
            }
            return tempResults;
        }

        public K Get<K, T>(System.Windows.Controls.ContentControl chain, int row, int column)
                where K : UIElement
                where T : FrameworkElement
        {
           return ((K)((StackPanel)chain.Content).Children[column]);
        }

        public UIElementCollection GetChildren(object container)
        {
            return (container as StackPanel).Children;
        }
    }
}
